//
//  HBHChouseAreaView.m
//  HubeiHotel
//
//  Created by 张露杰 on 18/3/28.
//  Copyright © 2018年 张露杰. All rights reserved.
//

#import "HBHChouseAreaView.h"

#import "HBHAreaCell.h"
#import "HBHtradingAreaCell.h"

typedef void(^HBHChouseAreaViewBlock)(NSDictionary *area,NSDictionary *tradingArea);

@interface HBHChouseAreaView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *areaTable;
@property (nonatomic, strong) UITableView *tradingAreaTable;
@property (nonatomic, strong) NSString *cityID;

@property (nonatomic, strong) BMKUserLocation *userLocation;
@property (nonatomic, retain) NSMutableArray *areas;
@property (nonatomic, retain) NSMutableArray *tradingAreas;
@property (nonatomic, copy) HBHChouseAreaViewBlock complete;

@property (nonatomic, retain) NSDictionary *selectArea;
@property (nonatomic, retain) NSDictionary *selectTradingArea;

@end

@implementation HBHChouseAreaView

- (instancetype)initUserLocation:(BMKUserLocation *)userLocation selectArea:(NSDictionary *)selectArea selectTradingArea:(NSDictionary *)selectTradingArea complete:(void(^)(NSDictionary *area,NSDictionary *tradingArea))complete
{
    self = [super init];
    if (self) {
        _userLocation = userLocation;
        _selectArea = selectArea;
        _selectTradingArea = selectTradingArea;
        _areas = [NSMutableArray new];
        _tradingAreas = [NSMutableArray new];
        if (complete) {
            _complete = complete;
        }
        [self creatView];
        [self getAreaByLa];
    }
    return self;
}

-(void)creatView
{
    self.frame = CGRectMake(0, 40*screenW_6, SCREENW, SCREENH - 64 - 40*screenW_6);
    /**
     底部遮罩
     */
    UIView *backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    backGroundView.backgroundColor = [UIColor blackColor];
    backGroundView.alpha = 0.5;
    [self addSubview:backGroundView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [backGroundView addGestureRecognizer:tap];
    [self addSubview:self.areaTable];
    [self addSubview:self.tradingAreaTable];
}

-(UITableView *)areaTable
{
    if (!_areaTable) {
        _areaTable = [[UITableView alloc]initWithFrame:CGRectMake(0,0, 100*screenW_6, SCREENW) style:UITableViewStylePlain];
        _areaTable.delegate = self;
        _areaTable.dataSource = self;
        _areaTable.showsHorizontalScrollIndicator = NO;
        _areaTable.showsVerticalScrollIndicator = NO;
        _areaTable.separatorStyle = 0;
        _areaTable.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _areaTable.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
    }
    return _areaTable;
}

-(UITableView *)tradingAreaTable
{
    if (!_tradingAreaTable) {
        _tradingAreaTable = [[UITableView alloc]initWithFrame:CGRectMake(100*screenW_6,0, SCREENW - 100*screenW_6, SCREENW) style:UITableViewStylePlain];
        _tradingAreaTable.delegate = self;
        _tradingAreaTable.dataSource = self;
        _tradingAreaTable.showsHorizontalScrollIndicator = NO;
        _tradingAreaTable.showsVerticalScrollIndicator = NO;
        _tradingAreaTable.separatorStyle = 0;
        _tradingAreaTable.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _tradingAreaTable.backgroundColor = [UIColor whiteColor];
    }
    return _tradingAreaTable;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _areaTable) {
        return _areas.count;
    }else if (tableView == _tradingAreaTable){
        return _tradingAreas.count;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _areaTable) {
        HBHAreaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBHAreaCell"];
        if (!cell) {
            cell = [[HBHAreaCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBHAreaCell"];
        }
        [cell loadCellWithData:_areas[indexPath.row] isSelect:[_selectArea[@"id"] isEqualToString:_areas[indexPath.row][@"id"]]];
        return cell;
    }else if (tableView == _tradingAreaTable){
        HBHtradingAreaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBHtradingAreaCell"];
        if (!cell) {
            cell = [[HBHtradingAreaCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBHtradingAreaCell"];
        }
        [cell loadCellWithData:_tradingAreas[indexPath.row] isSelect:[_selectTradingArea[@"id"] isEqualToString:_tradingAreas[indexPath.row][@"id"]]];
        return cell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40*screenW_6;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tradingAreaTable == tableView) {
        _selectTradingArea = _tradingAreas[indexPath.row];
        _complete(_selectArea,_selectTradingArea);
        [self dismiss];
    }else if (_areaTable == tableView){
        if (_areas[indexPath.row] != _selectArea) {
            _selectArea = _areas[indexPath.row];
            [tableView reloadData];
            [self tradingAreaList];
        }
    }
}


-(void)dismiss
{
    if (_disMissBlock) {
        _disMissBlock();
    }
    [self removeFromSuperview];
}

-(void)showOnView:(UIView *)view
{
    if (!view) {
        return;
    }
    [view addSubview:self];
}

#pragma mark - request
-(void)getAreaByLa
{
    [HudHelper showProgressHudWithMessage:@"" toView:self];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/other/getAreaByLa" Dic:@{@"parentId":[standfault objectForKey:@"cityID"]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([responseDic[@"success"] boolValue]) {
                    _areas = [NSMutableArray arrayWithArray:responseDic[@"data"]];
                    [_areas insertObject:@{@"name":@"附近",@"id":@""} atIndex:0];
                    if (!_selectArea) {
                        _selectArea = _areas[0];
                    }
                    [_areaTable reloadData];
                    [self tradingAreaList];
                }else{
                    [HudHelper hideHudToView:self];
                    [TomamaUI creatAlertWithTarget:[TomamaUI getTopVC] message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

-(void)tradingAreaList
{
    if ([_selectArea[@"id"] isEqualToString:@""]) {
        [HudHelper hideHudToView:self];
        _tradingAreas = [NSMutableArray arrayWithArray:@[@{@"name":@"附近",@"id":@""}]];
        [_tradingAreaTable reloadData];
    }else{
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [HttpRequest PostrequestUrl:@"/other/tradingAreaList" Dic:@{@"areaId":_selectArea[@"id"]} completion:^(NSDictionary *responseDic) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [HudHelper hideHudToView:self];
                    if ([responseDic[@"success"] boolValue]) {
                        _tradingAreas = [NSMutableArray arrayWithArray:responseDic[@"data"]];
                        [_tradingAreaTable reloadData];
                    }else{
                        [TomamaUI creatAlertWithTarget:[TomamaUI getTopVC] message:responseDic[@"msg"] timeInterval:2];
                    }
                });
            }];
        });
    }
}

@end
